/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.mobiubiq.directory.service.handlers;

import java.util.Stack;
import java.util.Vector;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.net.URLCodec;
import org.mobiubiq.directory.client.data.Service;
import org.mobiubiq.directory.client.data.Setting;
import org.mobiubiq.directory.client.object.AbstractObject;
import org.mobiubiq.directory.client.util.Base64;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 * 
 * @author Marnix Renne
 *
 */
public class FindServicesResponseHandler extends DefaultHandler {

    private Vector services;
    private StringBuffer buffer;
    private Service service;
    private AbstractObject object;
    private URLCodec codec;
    private Vector settings;
    private Stack stack;

    public FindServicesResponseHandler(AbstractObject object) {
        super();
        this.object = object;
        codec = new URLCodec();
        stack = new Stack();
    }

    /**
     * 
     * @return
     */
    public Vector getServices() {
        return this.services;
    }

    public void startDocument() throws SAXException {
        super.startDocument();
        this.services = new Vector();
    }

    public void characters(char[] chars, int offset, int length) throws SAXException {
        buffer.append(chars, offset, length);
    }

    public void startElement(String uri, String localname, String qname, Attributes attributes) throws SAXException {
        stack.push(qname);
        this.buffer = new StringBuffer();
        if (qname.equals("service")) {
            this.service = new Service(object);
        }
        if (qname.equals("settings")) {
            this.settings = new Vector();
        }
        if (qname.equals("setting")) {
            Setting setting = new Setting();
            setting.setDisplayName(attributes.getValue("displayName"));
            setting.setName(attributes.getValue("name"));
            try {
                Class typeClass = Class.forName(attributes.getValue("type"));
                setting.setType(typeClass);
            } catch (ClassNotFoundException ex) {
                // default to string class
                setting.setType(String.class);
            }
            settings.addElement(setting);
        }
    }

    public void endElement(String uri, String localname, String qname) throws SAXException {
        try {
            if (qname.equals("service")) {
                services.addElement(service);
            } else if (qname.equals("service-url")) {
                service.setUrl(codec.decode(buffer.toString()));
            } else if (qname.equals("id")) {
                service.setId(codec.decode(buffer.toString()));
            } else if (qname.equals("name")) {
                service.setName(codec.decode(buffer.toString()));
            } else if (qname.equals("company")) {
                service.setCompany(codec.decode(buffer.toString()));
            } else if (qname.equals("description")) {
                if (service != null) { // description of a service node
                    service.setDescription(codec.decode(buffer.toString()));                    
                } else {    // description of the object
                    object.setDescription(codec.decode(buffer.toString()));
                }
            } else if (qname.equals("title")) {
                object.setTitle(codec.decode(buffer.toString()));
            } else if(qname.equals("object-url")) {
                object.setObjectUrl(codec.decode(buffer.toString()));
            } else if(qname.equals("object-id")) {
                object.setObjectId(codec.decode(buffer.toString()));
            } else if (qname.equals("version")) {
                service.setVersion(codec.decode(buffer.toString()));
            } else if (qname.equals("icon-binary")) {
                String value = buffer.toString();
                byte[] data = Base64.decode(value);
                service.setIconBinary(data);
            } else if (qname.equals("settings")) {
                service.setSettings(settings);
            }
        } catch (DecoderException ex) {
            throw new SAXException(ex.getMessage());
        }
    }
}
